package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.JwtTool;
import com.jzo2o.customer.mapper.AgencyCertAuditMapper;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.AgencyCertificationAuditResDTO;
import com.jzo2o.customer.service.IAgencyCertAuditService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class IAgencyCertAuditServiceImpl extends ServiceImpl<AgencyCertAuditMapper, AgencyCertificationAudit> implements IAgencyCertAuditService {
    @Resource
    private JwtTool jwtTool;

    /**
     * 机构人员实名认证功能
     * @param dto 实名信息
     */
    @Override
    public void agencyRealNameAuthentication(AgencyCertificationAuditResDTO dto) {
        // 封装数据
        AgencyCertificationAudit agencyCertAudit = BeanUtils.copyBean(dto, AgencyCertificationAudit.class);
        agencyCertAudit.setCreateTime(new Date());
        baseMapper.insert(agencyCertAudit);
    }

    /**
     * 查询当前机构人员用户实名认证的驳回原因功能
     * @param request request
     * @return Map
     */
    @Override
    public Map<String, String> rejectReason(HttpServletRequest request) {
        // 从请求头中解析出token
        String token = request.getHeader("Authorization");
        // 根据token解析出当前登录的用户信息(userId)
        CurrentUserInfo user = jwtTool.parseToken(token);
        // 根据用户id查询用户实名认证的驳回原因
        AgencyCertificationAudit workerCertAudit = baseMapper.selectOne(
                new LambdaQueryWrapper<AgencyCertificationAudit>()
                        .eq(AgencyCertificationAudit::getServeProviderId, user.getId())
        );
        // 封装数据
        Map<String, String> mapResult = new HashMap<>();
        mapResult.put("rejectReason", workerCertAudit.getRejectReason());
        return mapResult;
    }

    /**
     * 分页查询机构人员实名认证信息接口
     * @param dto 分页参数
     * @return PageResult
     */
    @Override
    public PageResult<AgencyCertificationAudit> pageInfo(AgencyCertificationAuditPageQueryReqDTO dto) {
        return PageHelperUtils.selectPage(dto,
                () -> baseMapper.selectAgencyCertAuditPage(dto));
    }

    /**
     * 机构人员实名认证
     * @param id  id
     * @param wca 认证参数信息
     */
    @Override
    public void audit(Long id, WorkerCertificationAudit wca) {
        AgencyCertificationAudit agencyCertAudit = baseMapper.selectById(id);
        if(agencyCertAudit == null) {
            throw new ForbiddenOperationException("您所认证的实名信息不存在,请检查");
        }
        // 封装数据
        agencyCertAudit.setCertificationStatus(wca.getCertificationStatus());
        agencyCertAudit.setRejectReason(wca.getRejectReason());
        agencyCertAudit.setAuditStatus(1);
        // 更新
        baseMapper.updateById(agencyCertAudit);
    }
}
